package com.zzx.business.controller;

import com.zzx.business.service.ITbFilesService;
import com.zzx.business.vo.TbFilesVo;
import com.zzx.common.annotation.Log;
import com.zzx.common.core.controller.BaseController;
import com.zzx.common.core.domain.AjaxResult;
import com.zzx.common.core.page.TableDataInfo;
import com.zzx.common.enums.BusinessType;
import com.zzx.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 文件列表Controller
 *
 * @author Zzx
 * @date 2025-05-23
 */
@Api(tags = "文件列表Controller")
@RestController
@RequestMapping("/business/files")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TbFilesController extends BaseController {

    private final ITbFilesService tbFilesService;

    @ApiOperation("查询文件列表列表")
    @PreAuthorize("@ss.hasPermi('business:files:list')")
    @GetMapping("/list")
    public TableDataInfo<TbFilesVo> list(TbFilesVo entity) {
        return tbFilesService.queryList(entity);
    }

    @ApiOperation("查询文件列表所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TbFilesVo entity) {
        return AjaxResult.success("查询成功", tbFilesService.queryAll(entity));
    }

    @ApiOperation("导出文件列表列表")
    @PreAuthorize("@ss.hasPermi('business:files:export')")
    @Log(title = "文件列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbFilesVo entity) {
        List<TbFilesVo> list = tbFilesService.queryAll(entity);
        ExcelUtil<TbFilesVo> util = new ExcelUtil<>(TbFilesVo.class);
        util.exportExcel(response, list, "文件列表数据");
    }

    @ApiOperation("获取文件列表详细信息")
    @PreAuthorize("@ss.hasPermi('business:files:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tbFilesService.queryById(id));
    }

    @ApiOperation("新增文件列表")
    @PreAuthorize("@ss.hasPermi('business:files:add')")
    @Log(title = "文件列表", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TbFilesVo entity) {
        return toAjax(tbFilesService.saveFile(entity));
    }

    @ApiOperation("修改文件列表")
    @PreAuthorize("@ss.hasPermi('business:files:edit')")
    @Log(title = "文件列表", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TbFilesVo entity) {
        return toAjax(tbFilesService.updateFile(entity));
    }

    @ApiOperation("删除文件列表")
    @PreAuthorize("@ss.hasPermi('business:files:remove')")
    @Log(title = "文件列表", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbFilesService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
